﻿using FinancialManagement.Infrstructre;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FinancialManagement.Controllers
{
    [Route("api/[controller]")]
    [Authorize]
    [ApiController]
    public class FinancialController : ControllerBase
    {

        public SqlsugerDBContext db;
        public FinancialController(SqlsugerDBContext _db)
        {
            db = _db;
        }

        [HttpGet]
        public string InitDataBase()
        {
            db.CreateDatabase();
            return "数据库初始化成功";
        }

        /// <summary>
        /// 配置阿波罗
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Apollo")]
        public  IActionResult ApolloTest([FromServices] IConfiguration configuration,string key)
        {
            var elkConfig = configuration[key];
            return Ok(elkConfig);

        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UploadFile(IFormFile file)
        {
            string savePath = Directory.GetCurrentDirectory + @"\wwwroot\pic\";
            string newfilename = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);

            using(FileStream fs = new FileStream(savePath + newfilename, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
             return Ok(newfilename);
        }


    }
}
